using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;

public class Lesson2 : MonoBehaviour
{
    public RenderTexture renderTexture;
    public VideoClip clip;
    VideoPlayer videoPlayer;
    public bool isOver = false;
    void Start()
    {
        //注意：使用VideoPlayer组件需要引用命名空间 UnityEngine.Video

        //1.将一个 VideoPlayer 附加到主摄像机
        // 将 VideoPlayer 添加到摄像机对象时
        // 它会自动瞄准摄像机背板，无需更改videoPlayer.targetCamera。
        GameObject camera = GameObject.Find("Main Camera");
        videoPlayer = camera.AddComponent<VideoPlayer>();

        //2.参数相关
        //是否自动播放
        videoPlayer.playOnAwake = true;

        //渲染模式
        //videoPlayer.renderMode = VideoRenderMode.RenderTexture;
        //设置目标 渲染贴图
        //videoPlayer.targetTexture = renderTexture;
        //设置目标摄像机
        //videoPlayer.targetCamera

        //透明度
        videoPlayer.targetCameraAlpha = 0.5f;
        //videoPlayer.targetCamera3DLayout = Video3DLayout.OverUnder3D;

        //视频源
        videoPlayer.source = VideoSource.VideoClip;
        videoPlayer.clip = clip;

        videoPlayer.source = VideoSource.Url;
        videoPlayer.url = Application.streamingAssetsPath + "/zhuxian2.mp4";

        //是否循环 
        videoPlayer.isLooping = false;

        //3.方法相关
        //播放、停止、暂停

        //准备函数(准备视频数据,建议初始化时都调用)
        videoPlayer.Prepare();

        //4.事件相关
        //准备完成事件
        videoPlayer.prepareCompleted += (v) =>
        {
            isOver = true;
            Debug.LogFormat("准备完成事件...");
        };
        //开始事件
        videoPlayer.started += (v) =>
        {
            Debug.LogFormat("开始事件...");
        };
        //结尾时调用事件 
        videoPlayer.loopPointReached += (v) =>
        {
            Debug.LogFormat("结尾时调用事件...");
        };
    }

    // Update is called once per frame
    void Update()
    {
        //视频总时长/当前时长
        Debug.LogFormat("time {0}/{1}", videoPlayer.length, videoPlayer.time);
        //总帧数/当前帧
        Debug.LogFormat("frame {0}/{1}", videoPlayer.frameCount, videoPlayer.frame);
        
        //播放
        if (isOver && Input.GetKeyDown(KeyCode.Space))
        {
            videoPlayer.Play();
        }

        //停止
        if (isOver && Input.GetKeyDown(KeyCode.S))
        {
            videoPlayer.Stop();
        }

        //暂停
        if (isOver && Input.GetKeyDown(KeyCode.P))
        {
            videoPlayer.Pause();
        }
    }
}
